建立驗證信發送模組
public class MailService
{
private string mail_account = "";
private string mail_password = "";
private string mail_address = "";
//產生驗證碼方法
public string GetValidateCode()
{
string[] code = { "A", "B", "C", "D", "1", "2", "3", "4", "a", "b", "c", "d" };//驗證碼字元陣列
string ValidateCode = string.Empty;
Random rd = new Random();
for(int i = 0; i < 10; i++)
{
ValidateCode += code[rd.Next(code.Count())];
}
return ValidateCode;
}
public string GetRegisterMailBody(string TempString,string UserName,string ValidateUrl)
{
TempString = TempString.Replace("{{UserName}}", UserName);
TempString = TempString.Replace("{{ValidateUrl}}", ValidateUrl);
return TempString;
}
public void SendRegisterMail(string MailBody,string ToEmail)
{
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
SmtpServer.Port = 587;
SmtpServer.Credentials = new NetworkCredential(mail_account, mail_password);
SmtpServer.EnableSsl = true;
MailMessage mail = new MailMessage();
mail.From = new MailAddress(mail_address);
mail.To.Add(ToEmail);
mail.Subject = "註冊驗證信件";
mail.Body = MailBody;
mail.IsBodyHtml = true;
SmtpServer.Send(mail);
}
}